package cn.krui.helper.db;

/**
 * @author zc
 */
public class Parameter {
  public String parameterName;
  
  public Object parameterValue;
  
  public DirectionModel mode = DirectionModel.IN;
  
  public String jdbcType;
  
  public String javaType;
  
  public Integer numericScale = null;
  
  public String typeHandler;
  
  public Parameter(String parameterName, Object parameterValue, DirectionModel mode, String jdbcType, String javaType, Integer numericScale, String typeHandler) {
    this.parameterName = parameterName;
    this.parameterValue = parameterValue;
    this.mode = mode;
    this.jdbcType = jdbcType;
    this.javaType = javaType;
    this.numericScale = numericScale;
    this.typeHandler = typeHandler;
  }
  
  public String toString() {
    StringBuilder sql = new StringBuilder();
    toString(sql);
    return sql.toString();
  }
  
  public void toString(StringBuilder sql) {
    if (this.mode != null && this.mode == DirectionModel.RETURN){
      return; 
    }
    if (this.parameterValue instanceof SQLPart) {
      sql.append(this.parameterValue.toString());
      return;
    } 
    sql.append("#{").append(this.parameterName);
    if (this.mode != null && this.mode != DirectionModel.IN){
      sql.append(',').append("mode=").append(this.mode.name()); 
    }
    if (this.jdbcType != null && this.jdbcType.length() > 0){
      sql.append(',').append("jdbcType=").append(this.jdbcType); 
    }
    if (this.javaType != null && this.javaType.length() > 0){
      sql.append(',').append("javaType=").append(this.javaType); 
    }
    if (this.numericScale != null && this.numericScale.intValue() > 0){
      sql.append(',').append("numericScale=").append(this.numericScale); 
    }
    if (this.typeHandler != null && this.typeHandler.length() > 0){
      sql.append(',').append("typeHandler=").append(this.typeHandler); 
    }
    sql.append('}');
  }
  
  public void toString(StringBuilder sql, DirectionModel mode) {
    if (this.parameterValue instanceof SQLPart) {
      sql.append(this.parameterValue.toString());
      return;
    } 
    sql.append("#{").append(this.parameterName);
    if (mode != null && mode != DirectionModel.IN){
      sql.append(',').append("mode=").append(mode.name()); 
    }
    if (this.jdbcType != null && this.jdbcType.length() > 0){
      sql.append(',').append("jdbcType=").append(this.jdbcType); 
    }
    if (this.javaType != null && this.javaType.length() > 0){
      sql.append(',').append("javaType=").append(this.javaType); 
    }
    if (this.numericScale != null && this.numericScale.intValue() > 0){
      sql.append(',').append("numericScale=").append(this.numericScale); 
    }
    if (this.typeHandler != null && this.typeHandler.length() > 0){
      sql.append(',').append("typeHandler=").append(this.typeHandler); 
    }
    sql.append('}');
  }
}
